$("#edit").click(function() {
 	 List();
});

var addata='';		//广告资料
var statData='';	//统计资料

function CommaNum(number){
	 var num = number.toString();
	 var pattern = /(-?\d+)(\d{3})/;
	 while(pattern.test(num)){
		 num = num.replace(pattern, "$1,$2");
	 }
	 return num;
}

function List() {
	var postUrl = 'List';
	var dataSend = {
	};
	$.ajax({
		url : postUrl,
		type : "post",
		dataType : "json",
		data : dataSend,
		success : function(data) {
			console.log(data.message);
			console.log("连接 Servlet 服务成功,"+ postUrl + " request status is " + data.status);
			if(data.status){
				console.log("AdvDetailList: " + data.AdvDetailList.length);
				addata = data;
				$("#advAccountMoney").text(CommaNum(data.accountMoney));
				showAD();
				$(".fancytable tr").click(function(){
					var index = $(this).attr("index");
					ClickUpdateDetail(index);
				});
			}else{
				console.log("failure");
				dialog("资料错误",2000);
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			dialog("网路连线异常",2000);
			console.log("return fail");
		}
	});
}

// 广告清单
function showAD(){
	var detailPage='';
	detailPage +=' <table class="fancytable"> ';
//	detailPage +=' '
//				+' <td>advmentId</td> '		
//				+' <td width="150">广告名称</td> '
//				+' <td>advDesc</td> ' 
//				+' <td>advKeyword</td> '
//				+' <td width="50">点阅数</td> '
//				+' <td>advGrade</td> '
//				+' <td width="70">刊登位置</td> '
//				+' <td>advLocId</td> '				
//				+' <td>advLocation</td> '
//				+' <td>advBudget</td> '
//				+' <td>advTimes</td> '
//				+' <td>advBonus</td> '
//				+' <td>advVideoUrl</td> '
//				+' <td>advPhotoUrl</td> '
//				+' <td>advCouponTitle</td> '
//				+' <td>advCouponUrl</td> '
//				+' <td>advCouponAmount</td> '
//				+' <td>advCouponGetAmount</td> '				
//				+' <td width="130">上架时间</td> '
//				+' <td width="140">下架时间</td> '
//				+' <td>advCriticable</td> '
//				+' <td width="35">状态</td> '
//				+' <td>advLength</td> '
//				+' <td>advTag</td> '
//				+' <td>advId</td> '
//				+' <td width="30">功能</td> '				
//	;
	
	var Status = '', Position = '', totalAdvBudget = 0;
	for (i = 0; i < addata.AdvDetailList.length; i++) {
		Status = addata.AdvDetailList[i].advStatus;
		if(Status == 0){
			Status = "审核中";
		}else if(Status == 1){
			Status = "通过";
		}else if(Status == 3){
			Status = "失败";
		}
		detailPage +=' <tr index="'+i+'"> ';
//		detailPage +=' <td>'+addata.AdvDetailList[i].advmentId+'</td> ';
		detailPage +=' <td width="150">'+addata.AdvDetailList[i].advName+'</td> ';
//		detailPage +=' <td>'+addata.AdvDetailList[i].advDesc+'</td> ' ;
//		detailPage +=' <td>'+addata.AdvDetailList[i].advKeyword+'</td> ';
		detailPage +=' <td width="50">'+CommaNum(addata.AdvDetailList[i].advViews)+'</td> ';
//		detailPage +=' <td>'+addata.AdvDetailList[i].advGrade+'</td> ';
//		detailPage +=' <td>'+Position+'</td> ';
//		detailPage +=' <td>'+addata.AdvDetailList[i].advLocId+'</td> ';		
//		detailPage +=' <td>'+addata.AdvDetailList[i].advLocation+'</td> ';
//		detailPage +=' <td>'+addata.AdvDetailList[i].advBudget+'</td> ';
//		detailPage +=' <td>'+addata.AdvDetailList[i].advTimes+'</td> ';
//		detailPage +=' <td>'+addata.AdvDetailList[i].advBonus+'</td> ';
//		detailPage +=' <td>'+addata.AdvDetailList[i].advVideoUrl+'</td> ';
//		detailPage +=' <td>'+addata.AdvDetailList[i].advPhotoUrl+'</td> ';
//		detailPage +=' <td>'+addata.AdvDetailList[i].advCouponTitle+'</td> ';
//		detailPage +=' <td>'+addata.AdvDetailList[i].advCouponUrl+'</td> ';
//		detailPage +=' <td>'+addata.AdvDetailList[i].advCouponAmount+'</td> ';
//		detailPage +=' <td>'+addata.AdvDetailList[i].advCouponGetAmount+'</td> ';
//		detailPage +=' <td>'+addata.AdvDetailList[i].advOnshtime+'</td> ';
//		detailPage +=' <td>'+addata.AdvDetailList[i].advOffshtime+'</td> ';
//		detailPage +=' <td>'+addata.AdvDetailList[i].advCriticable+'</td> ';
		detailPage +=' <td width="35">'+Status+'</td> ';
//		detailPage +=' <td>'+addata.AdvDetailList[i].advLength+'</td> ';
//		detailPage +=' <td>'+addata.AdvDetailList[i].advTag+'</td> ';
//		detailPage +=' <td>'+addata.AdvDetailList[i].advId+'</td> ';
		detailPage +=' <td width="30"><input value=\''+JSON.stringify(addata.AdvDetailList[i])+'\' hidden=""><input type="button" onclick="MNG_UpdateAdvment('+i+')" value="编辑" /></td> ';
		detailPage +=' </tr> ';
		totalAdvBudget += addata.AdvDetailList[i].advBudget;
	}
	detailPage +=' </table> ';
	//count total budget
	$("#totalAdvBudget").text(CommaNum(totalAdvBudget));
	$("#adList").html(detailPage);
	
	if(addata.AdvDetailList.length>0){
//		var Position = addata.AdvDetailList[0].advPosition;	
//		if(Position == 1){
//			Position = "入口首页";
//		}else if(Position == 2){
//			Position = "顶层轮播";
//		}else if(Position == 3){
//			Position = "一般区块";
//		}
//		
//		$("#advId").text(addata.AdvDetailList[0].advId);
//		$("#advTag").text(addata.AdvDetailList[0].advTag);
//		$("#advmentId").text(addata.AdvDetailList[0].advmentId);
//		$("#advCriticable").text(addata.AdvDetailList[0].advCriticable);
//		$("#advStatus").text(addata.AdvDetailList[0].advStatus);
//		$("#advGrade").text(addata.AdvDetailList[0].advGrade);
//		$("#advViews").text(CommaNum(addata.AdvDetailList[0].advViews));
//		$("#advName").text(addata.AdvDetailList[0].advName);
//		$("#advDesc").text(addata.AdvDetailList[0].advDesc);
//		$("#advKeyword").text(addata.AdvDetailList[0].advKeyword);
//		$("#advVideoUrl").attr("src",addata.AdvDetailList[0].advVideoUrl);
//		$("#advPhotoUrl").attr("src",addata.AdvDetailList[0].advPhotoUrl);
//		$("#advCouponTitle").text(addata.AdvDetailList[0].advCouponTitle);
//		$("#advCouponUrl").attr("src",addata.AdvDetailList[0].advCouponUrl);
//		$("#advCouponGetAmount").text(CommaNum(addata.AdvDetailList[0].advCouponGetAmount));
//		$("#advBudget").text(CommaNum(addata.AdvDetailList[0].advBudget));
//		$("#advRemainBudget").text(CommaNum(addata.AdvDetailList[0].advRemainBudget));
//		$("#advConsumeBudget").text(CommaNum(addata.AdvDetailList[0].advBudget-addata.AdvDetailList[0].advRemainBudget));
//		$("#advBonus").text(addata.AdvDetailList[0].advBonus);
//		$("#advPosition").text(Position);
//		$("#advLocation").text(addata.AdvDetailList[0].advLocation);
//		$("#advTimes").text(CommaNum(addata.AdvDetailList[0].advTimes));
//		$("#advOnshtime").text(addata.AdvDetailList[0].advOnshtime);
//		$("#advOffshtime").text(addata.AdvDetailList[0].advOffshtime);
//		$("#AdvStatChart").empty();
//		statReport(addata.AdvDetailList[0].advmentId);
		ClickUpdateDetail(0);
	}
	
};

//var index=0;
//点击列表项目更新详细资讯
function ClickUpdateDetail(index){
	var Position = addata.AdvDetailList[index].advPosition;	
	if(Position == 1){
		Position = "入口首页";
	}else if(Position == 2){
		Position = "顶层轮播";
	}else if(Position == 3){
		Position = "一般区块";
	}
	
	var Status = addata.AdvDetailList[index].advStatus;
	if(Status == 0){
		Status = "审核中";
	}else if(Status == 1){
		Status = "通过";
	}else if(Status == 3){
		Status = "失败";
	}
	
	$("#advId").text(addata.AdvDetailList[index].advId);
	$("#advTag").text(addata.AdvDetailList[index].advTag);
	$("#advmentId").text(addata.AdvDetailList[index].advmentId);
	$("#advCriticable").text(addata.AdvDetailList[index].advCriticable);
	$("#advStatus").text(addata.AdvDetailList[index].advStatus);
	$("#advGrade").text(addata.AdvDetailList[index].advGrade);
	$("#advViews").text(CommaNum(addata.AdvDetailList[index].advViews));
	$("#advName").text(addata.AdvDetailList[index].advName);
	$("#advDesc").text(addata.AdvDetailList[index].advDesc);
	$("#advKeyword").text(addata.AdvDetailList[index].advKeyword);
	$("#advVideoUrl").attr("src",addata.AdvDetailList[index].advVideoUrl);
	$("#advPhotoUrl").attr("src",addata.AdvDetailList[index].advPhotoUrl);
	$("#advCouponTitle").text(addata.AdvDetailList[index].advCouponTitle);
	$("#advCouponUrl").attr("src",addata.AdvDetailList[index].advCouponUrl);
	$("#advCouponGetAmount").text(CommaNum(addata.AdvDetailList[index].advCouponGetAmount));
	$("#advBudget").text(CommaNum(addata.AdvDetailList[index].advBudget));
	$("#advRemainBudget").text(CommaNum(addata.AdvDetailList[index].advRemainBudget));
	$("#advConsumeBudget").text(CommaNum(addata.AdvDetailList[index].advBudget-addata.AdvDetailList[index].advRemainBudget));
	$("#advBonus").text(addata.AdvDetailList[index].advBonus);
	$("#advPosition").text(Position);
	$("#advLocation").text(addata.AdvDetailList[index].advLocation);
	$("#advTimes").text(CommaNum(addata.AdvDetailList[index].advTimes));
	$("#advOnshtime").text(addata.AdvDetailList[index].advOnshtime);
	$("#advOffshtime").text(addata.AdvDetailList[index].advOffshtime);
	$("#AdvStatChart").empty();
	statReport(addata.AdvDetailList[index].advmentId);
}

/* 编辑广告 */
function MNG_UpdateAdvment(i) {
	var postUrl = 'ModifyAdvControl';
	var dataSend = {
		advData : $('[index='+i+'] input').val()
//		advId: addata.AdvDetailList[i].advId,
//		advTag: addata.AdvDetailList[i].advTag,
//		advmentId: addata.AdvDetailList[i].advmentId,
//		advCriticable: addata.AdvDetailList[i].advCriticable,
//		advStatus: addata.AdvDetailList[i].advStatus,
//		advGrade: addata.AdvDetailList[i].advGrade,
//		advViews: addata.AdvDetailList[i].advViews,
//		advName: addata.AdvDetailList[i].advName,
//		advDesc: addata.AdvDetailList[i].advDesc,
//		advKeyword: addata.AdvDetailList[i].advKeyword,
//		advVideoUrl: addata.AdvDetailList[i].advVideoUrl,
//		advPhotoUrl: addata.AdvDetailList[i].advPhotoUrl,
//		advCouponTitle: addata.AdvDetailList[i].advCouponTitle,
//		advCouponUrl: addata.AdvDetailList[i].advCouponUrl,
//		advBudget: addata.AdvDetailList[i].advBudget,
//		advBonus: addata.AdvDetailList[i].advBonus,
//		advPosition: addata.AdvDetailList[i].advPosition,
//		advLocation: addata.AdvDetailList[i].advLocation,
//		advTimes: addata.AdvDetailList[i].advTimes,
//		advOnshtime: addata.AdvDetailList[i].advOnshtime,
//		advOffshtime: addata.AdvDetailList[i].advOffshtime
	};
	$.ajax({
		url : postUrl,
		type : "post",
		dataType : "json",
		data : dataSend,
		success : function(data) {
			if(data.status){
				console.log("success to EDIT");
				window.location.assign(data.redirectPage);
				
			}else{
				console.log("failure to EDIT");
				dialog("资料错误",2000);
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			dialog("网路连线异常",2000);
			console.log("return fail");
		}
	});
}

function statReport(i) {
	var postUrl = 'ClickReport';
	var dataSend = {
		advmentId: i,
		type: $("input[name=statType]:checked").val(),
	};
	$.ajax({
		url : postUrl,
		type : "post",
		dataType : "json",
		data : dataSend,
		success : function(data) {
			console.log(data.message);
			console.log("连接 Servlet 服务成功,"+ postUrl + " request status is " + data.status);
			if(data.status){
				statData = data.AdvClickList;
				console.log("AdvClickList: " + data.AdvClickList.length);
				$.ajax({
					  url: 'js/chart.js',
					  dataType: "script",
				});
			}else{
				console.log("failure");
				dialog("资料错误",2000);
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			dialog("网路连线异常",2000);
			console.log("return fail");
		}
	});
}
